/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package shoppingservice.view;

import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import javafx.scene.input.MouseEvent;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import shoppingservice.Main;

/**
 * View used to display information to user
 */

public class MessageView extends CustomNode { 
    
    public var x = 2;
    public var y = 37;
    public var height = 248.0;
    public var width = 237.0;
    
    public var show = true on replace {
        fader(); 
    }
    
    public var text = "Please enter a product to search in above text box." on replace {
        if(not visible) { show = true; }
    }

    var bgRect = Rectangle {
        x: 0 y: 0
        width: width height: height
        fill: Color.TRANSPARENT
    }
                    
    var messageRect = Rectangle {
        x: 7 y: 7
        width: width-14 height: height-14
        arcWidth: 8 arcHeight: 8
        fill: Color.rgb(35, 35, 35)
        stroke: Color.rgb(30, 39, 47)
        strokeWidth: 2.0
        opacity: 0.5
    }
    
    var attributionText = Text {
        x: 23
        y: height - 20
        font: Font { name: "Bitstream Vera Sans Bold" size: 10 }
        wrappingWidth: 220
        fill: Color.WHITE
        content: "Powered by SmarterRetail WebService!"
    };
        
    public var messageOpacity = 1.0;
    var message = Text {
        x: 16
        y: 120
        font: Font { name: "Bitstream Vera Sans Bold" size: 12 }
        wrappingWidth: 200
        fill: Color.WHITE
        content: bind text
    };
    
    override function create():Node {
        
        blocksMouse = true;

        Group {
            translateX: bind x
            translateY: bind y
            content: [ bgRect, messageRect, message, attributionText ]
            opacity: bind messageOpacity
        };
    }

    override var onMousePressed = function(e : MouseEvent) {
        Main.stageDragInitialX = e.screenX - Main.stage.x;
        Main.stageDragInitialY = e.screenY - Main.stage.y;
    }

    override var onMouseDragged = function(e : MouseEvent) {
        Main.stage.x = e.screenX - Main.stageDragInitialX;
        Main.stage.y = e.screenY - Main.stageDragInitialY;
    }

    /**
     * Make message-view appear slowly by changing opacity
     */
    function fader() {
        
        var endOpacity = 0.0;
        if(show) {
            messageOpacity = 0.0;
            endOpacity = 1.0;
            visible = true;
        } else {
            messageOpacity = 1.0;
            endOpacity = 0.0;
        }
        
        var faderTimeline:Timeline = Timeline {
            keyFrames: [ 
                KeyFrame {
                    time: 1s
                    values: [ messageOpacity => endOpacity tween Interpolator.LINEAR ] 
                    action: function() {
                        if(endOpacity == 0.0) {
                            visible = false;
                        }
                    }
                }
            ]
        };
        faderTimeline.playFromStart();
    }
}
